package com.mxprojects.gui
{
	import com.mxprojects.constructors.ButtonLarge;
	import com.mxprojects.constructors.DrawBorder;
	import com.mxprojects.constructors.ImageDisplay;
	import com.mxprojects.constructors.InputConstructor;
	import com.mxprojects.constructors.SimpleTextField;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.FileUploader;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	
	import flash.display.Sprite;
	import flash.net.FileReference;
	import flash.text.TextFieldAutoSize;
	
	public class AvatarDisplay extends Sprite
	{
		protected static const UPLOAD_BAR_WIDTH:int = 100;
		
		private static var avatarFile:FileItem;
		private static var avatarUpload:FileUploader;
		private static var avatarPreview:Sprite;
		private static var avatarImage:ImageDisplay;
		private static var avatarHelp:SimpleTextField;
		private static var uploadButton:ButtonLarge;
		
		private var uploaderWidth:int;
		
		public function AvatarDisplay(){}
		
		
		public function setupAvatarUploader(uploaderWidth:int, avatarURL:String = ""):Sprite {
			this.uploaderWidth = uploaderWidth;
			
			//create the avatar preview box thing
			createPreview();
			
			//setup the stuff that handles the upload
			avatarUpload = new FileUploader(addAvatar, true, Config.MAX_AVATAR_FILE_SIZE, 1, Config.MAX_AVATAR_WIDTH, Config.MAX_AVATAR_HEIGHT, Config.AVATAR_UPLOAD_SUB_DIRECTORY, true);

			//create the upload button
			if(avatarURL != ""){
				uploadButton = new ButtonLarge(Language.main(Word.CHANGE_AVATAR), avatarUpload.setupUploadButton);
				showAvatarImage(avatarURL);
			}else{
				uploadButton = new ButtonLarge(Language.main(Word.UPLOAD_AVATAR), avatarUpload.setupUploadButton);
			}
			uploadButton.x = Math.round(avatarPreview.x + avatarPreview.width/2 - uploadButton.width/2);
			uploadButton.y = avatarPreview.y + avatarPreview.height + Config.TEXT_PADDING;
			addChild(uploadButton);
			
			//put a label beside the preview
			var avatarLabel:SimpleTextField = new SimpleTextField("<span class=\"registerLabel\">" + 
																  Language.main(Word.AVATAR) + InputConstructor.LABEL_SEPARATOR + 
																  "</span>");
			avatarLabel.x = avatarPreview.x - avatarLabel.width;
			avatarLabel.y = avatarPreview.y;
			addChild(avatarLabel);
			
			//put a little descriptive text about the avatar upload
			var helpString:String = "<span class=\"avatarUploaderHelpBase\">" + Language.main(Word.AVATAR_RULES) + "<br><br>";
			helpString += Language.main(Word.MAXIMUM_DIMENSIONS) + ": <span class=\"avatarUploaderHelpDimensions\">" + Config.MAX_AVATAR_WIDTH + " x " + Config.MAX_AVATAR_HEIGHT + "</span><br>";
			helpString += Language.main(Word.MAXIMUM_FILE_SIZE) + ": <span class=\"avatarUploaderHelpFileSize\">" + Config.MAX_AVATAR_FILE_SIZE + " KB</span></span>";
			
			if(avatarHelp == null){
				var avatarHelp:SimpleTextField = new SimpleTextField("", 2, false, uploaderWidth - avatarPreview.width - Config.TEXT_PADDING);
			}
			avatarHelp.x = avatarPreview.x + avatarPreview.width + Config.TEXT_PADDING;
			avatarHelp.y = avatarPreview.y;
			avatarHelp.autoSize = TextFieldAutoSize.CENTER;
			avatarHelp.htmlText = helpString;
			
			addChild(avatarHelp);
			
			return this;
		}
		
		public function displayAvatar(avatarURL:String):Sprite {
			//create a holder for an avatar
			var avatarHolder:Sprite = createAvatarPreview();
			
			//load the avatar if they have one
			if(avatarURL != ""){
				var avatarImage:ImageDisplay = new ImageDisplay(avatarURL, "", Config.MAX_AVATAR_WIDTH, Config.MAX_AVATAR_HEIGHT);
				avatarHolder.addChild(avatarImage);				
			}
			
			return avatarHolder;
		}
		
		public function cleanUp():void {
			//this just cleans up any left over things that may be left around for the next time we are here
			avatarFile = null;
			avatarUpload = null;
			avatarPreview = null;
			avatarImage = null;
			avatarHelp = null;
		}
		
		public function getAvatarFile():FileItem {
			return avatarFile;
		}
		
		private function createAvatarPreview():Sprite {
			//build the holder
			var preview:Sprite = new Sprite();
			preview.graphics.beginFill(Styles.colorToUint(".avatarHolder"));
			preview.graphics.drawRect(0,0,Config.MAX_AVATAR_WIDTH,Config.MAX_AVATAR_HEIGHT);
			preview.graphics.endFill();
			
			//add a border
			var avatarBorder:Sprite = DrawBorder.create(preview, Styles.colorToUint(".avatarHolder", "borderColor"));
			preview.addChild(avatarBorder);
			
			return preview;
		}
		
		private function addAvatar(file:FileReference, selectedNumber:int, showProgress:Boolean):void {
			avatarFile = new FileItem(file, avatarUpload, UPLOAD_BAR_WIDTH, Config.AVATAR_UPLOAD_SUB_DIRECTORY, showProgress, false, true);
			avatarFile.x = avatarPreview.x;
			avatarFile.y = uploadButton.y;
			
			//listen for file remove to bring back the upload button
			avatarFile.addEventListener(FileItem.ADDED_FILE, showAvatar, false, 0, true);
			avatarFile.addEventListener(FileItem.REMOVED_FILE, bringBackUpload, false, 0, true);			
		}
		
		private function showAvatar(event:CustomEvent):void {
			//hide the upload button and add the file item to the holder
			uploadButton.visible = false;
			addChild(avatarFile);
			
			//display the newly uploaded avatar
			showAvatarImage(Config.INSTALL_DIRECTORY + Config.IMAGE_DIRECTORY + Config.AVATAR_UPLOAD_SUB_DIRECTORY + avatarFile.getFileName());
		}
		
		private function showAvatarImage(url:String):void {
			//show the avatar in the holder
			avatarImage = new ImageDisplay(url, "", Config.MAX_AVATAR_WIDTH,  Config.MAX_AVATAR_HEIGHT);
			avatarImage.x = avatarPreview.x;
			avatarImage.y = avatarPreview.y;
			avatarImage.mask = avatarPreview;
			addChild(avatarImage);
		}
		
		private function bringBackUpload(event:CustomEvent):void {
			uploadButton.visible = true;
			createPreview();
			
			//remove the pic of the avatar that was uploaded and the file reference
			removeChild(avatarImage);
			avatarFile = null;
		}
		
		private function createPreview():void {
			avatarPreview = createAvatarPreview();
			//avatarPreview.x = Math.round(uploaderWidth/5 - avatarPreview.width/2);
			addChild(avatarPreview);
		}
	}
}